//获取传过来的pid
console.log(location.search); //?pid=504536
//截取字符串
let obj = new URLSearchParams(location.search);
let pid = obj.get("pid");
console.log(pid); //504536
//通过商品id获取商品的详情
axios.get(productDetailAPI, { params: { id: pid } }).then((res) => {
    let pObj = res.data.data;
    console.log(pObj);
    let html = `
   <div class="content-left fl">
            <img src="${pObj.pimg}" alt="">
        </div>
        <div class="content-right fl">
            <h2>${pObj.pname}</h2>
            <p class="txt"><span>售价</span><i>￥</i> ${pObj.pprice}</p>
            <p class="txt"><span class="xx">评价</span>（已有520人评价）</p>
            <p class="txt"><span>配送</span>现货，现在下单，立即发货</p>
            <p class="txt"><span>数量</span>
            <button onclick =  "subBtn(${pObj.pid},this)"> - </button>
             <input type="text" value=1 class = "ipts" onblur = "iptBtn(${pObj.pid},this)" class = "pnum-ipt">
            <button onclick =  "addBtn(${pObj.pid},this)">+</button></p>
            <button onclick =  "carBtn(${pObj.pid})" class="btn">加入购物车</button>
        </div>
    `;
    let content = document.querySelector(".content");
    content.innerHTML = html;
});

//加按钮事件
function addBtn(pid, btn) {
    console.log("加");
    let ipt = btn.previousElementSibling;
    console.log(ipt.value);
    let pnum = parseInt(ipt.value) + 1;
    console.log(pnum);
    // let uid = localStorage.getItem("uid");
    // let params = { uid, pid, pnum };
    // let data = await axios.get(carUpdateAPI, { params });
    // console.log(data);
    ipt.value = pnum;
    console.log(ipt.value);

}

//减按钮事件
function subBtn(pid, btn) {
    console.log("减");
    console.log("减");
    console.log(btn);
    let ipt = btn.nextElementSibling;
    if (parseInt(ipt.value) == 1) {
    } else {
      let pnum = parseInt(ipt.value) - 1;
    //   let uid = localStorage.getItem("uid");
    //   let params = { uid, pid, pnum };
    //   let data = await axios.get(carUpdateAPI, { params });
    //   console.log(data);
      ipt.value = pnum;
    //   contFN();
    }
}


async function carBtn(pid) {
    console.log(pid);
    // let uid = localStorage.getItem("uid");
    let uid = 63073;
    let ipts = document.querySelector(".ipts");
    console.log(uid);
    let username = localStorage.getItem("username")
    let user = document.querySelector("#user");
    if (username == null) {
        location.href = "cf登录页.html";
    } else {
        //  uid  用户id 
        //  pid  商品id
        //  pnum  要添加的商品数量
        let pnum = parseInt(ipts.value);
        let params = { uid, pid, pnum };
        console.log(pnum);
        let { data } = await axios.get(CartAddAPI, { params });
        console.log(data);
        console.log(data.msg);
        alert("加入购物车成功")
    }
}


 //给输入框添加事件，失焦时触发，获取输入框的值改变购物车里面的数量
 async function iptBtn(pid, btn) {
    console.log(222);
    console.log(btn);
    let ipt = btn;
    console.log(ipt.value);
    console.log(pid);
    let pnum = parseInt(ipt.value);
    let uid = localStorage.getItem("uid");
    let params = { uid, pid, pnum };
    let data = await axios.get(CartUpdateAPI, { params });
    console.log(data);
    ipt.value = pnum;
    // contFN();
  }

// 给加入购物车添加点击事件
// async function carBtn(pid) {
//     console.log(11111);
//     console.log(pid);
//       let ipt = document.querySelector(".cart-add");
//       //   uid  用户id
//       //   pid  商品id
//       //   pnum  要添加的商品数量
//       let uid = localStorage.getItem("uid");
//       let pnum = parseInt(ipt.value);
//       let params = { uid, pid, pnum };
//       let {data} = await axios.get(CartAddAPI,{params});
//       console.log(data);
//       console.log(data.msg);
//     location.href = "cf购物车.html";


// }